package com.example.helloworld.widget;

import java.util.Calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

import com.example.helloworld.R;

public class DateTimePicker extends LinearLayout {

	public interface OnDateTimeChangedListener {
		void onDateTimeChanged(DateTimePicker view, int year, int monthOfYear, int dayOfYear, int hourOfDay, int minute);
	}
	
	private OnDateTimeChangedListener onDateTimeChangedListener;
	
	private final DatePicker datePicker;
	private final CheckBox enableTimeCheckBox;
	private final TimePicker timePicker;
	
	public DateTimePicker(Context context) {
		this(context, null);
	}
	
	public DateTimePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		LayoutInflater inflater =	(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.datetimepicker, this, true);
		
		Calendar calendar =	Calendar.getInstance();
		TypedArray array =	context.obtainStyledAttributes(attrs, R.styleable.DataTimePicker);
		final int _currentYear =	array.getInt(R.styleable.DataTimePicker_year, calendar.get(Calendar.YEAR));
		final int _currentMonth =	array.getInt(R.styleable.DataTimePicker_month, calendar.get(Calendar.MONTH));
		final int _currentDay =		array.getInt(R.styleable.DataTimePicker_day, calendar.get(Calendar.DAY_OF_MONTH));
		final int _currentHour =	array.getInt(R.styleable.DataTimePicker_hour, calendar.get(Calendar.HOUR_OF_DAY));
		final int _currentMinute =	array.getInt(R.styleable.DataTimePicker_minute, calendar.get(Calendar.MINUTE));
		
		datePicker =	(DatePicker)findViewById(R.id.datePicker);
		datePicker.init(_currentYear, _currentMonth, _currentDay, new OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
				if (onDateTimeChangedListener != null) {
					onDateTimeChangedListener.onDateTimeChanged(
						DateTimePicker.this, year, monthOfYear, dayOfMonth, timePicker.getCurrentHour().intValue(), timePicker.getCurrentMinute().intValue()
					);
				}
				
			}
		});
		
		enableTimeCheckBox =	(CheckBox)findViewById(R.id.enableTimeCheckBox);
		enableTimeCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				timePicker.setEnabled(arg1);
				timePicker.setVisibility(enableTimeCheckBox.isChecked() ? View.VISIBLE : View.INVISIBLE);
			}
		});
		
		timePicker =	(TimePicker)findViewById(R.id.timePicker);
		timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				if (onDateTimeChangedListener != null) {
					onDateTimeChangedListener.onDateTimeChanged(
						DateTimePicker.this, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), hourOfDay, minute
					);
				}
			}
			
		});
		
		timePicker.setCurrentHour(_currentHour);
		timePicker.setCurrentMinute(_currentMinute);
		timePicker.setEnabled(enableTimeCheckBox.isChecked());
		timePicker.setVisibility(enableTimeCheckBox.isChecked() ? View.VISIBLE : View.INVISIBLE);
	}
	
	public void setOnDateTimeChangedListner(OnDateTimeChangedListener onDateTimeChangedListener) {
		this.onDateTimeChangedListener =	onDateTimeChangedListener;
	}
	
	public void updateDateTime(int year, int monthOfYear, int dayOfMonth, int currentHour, int currentMinute) {
		datePicker.updateDate(year, monthOfYear, dayOfMonth);
		timePicker.setCurrentHour(currentHour);
		timePicker.setCurrentMinute(currentMinute);
	}

	public void updateDate(int year, int monthOfYear, int dayOfMonth) {
		datePicker.updateDate(year, monthOfYear, dayOfMonth);
	}
	
	public void setIs24HourView(final boolean is24HourView) {
		timePicker.setIs24HourView(is24HourView);
	}
	
	public int getYear() {
		return datePicker.getYear();
	}
	
	public int getMonth() {
		return datePicker.getMonth();
	}
	
	public int getDayOfMonth() {
		return datePicker.getDayOfMonth();
	}
	
	public int getCurrentHour() {
		return timePicker.getCurrentHour();
	}
	
	public int getCurrentMinute() {
		return timePicker.getCurrentMinute();
	}
	
	public boolean enableTime() {
		return enableTimeCheckBox.isChecked();
	}
}
